package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.BaseDataMemberMapper;
import com.ruoyi.cderp.domain.BaseDataMember;
import com.ruoyi.cderp.service.IBaseDataMemberService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 会员Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class BaseDataMemberServiceImpl implements IBaseDataMemberService
{
    @Autowired
    private BaseDataMemberMapper baseDataMemberMapper;

    LoginUser loginUser = null;

    /**
     * 查询会员
     *
     * @param id 会员主键
     * @return 会员
     */
    @Override
    public BaseDataMember selectBaseDataMemberById(String id)
    {
        return baseDataMemberMapper.selectBaseDataMemberById(id);
    }

    /**
     * 查询会员列表
     *
     * @param baseDataMember 会员
     * @return 会员
     */
    @Override
    public List<BaseDataMember> selectBaseDataMemberList(BaseDataMember baseDataMember)
    {
        return baseDataMemberMapper.selectBaseDataMemberList(baseDataMember);
    }

    /**
     * 新增会员
     *
     * @param baseDataMember 会员
     * @return 结果
     */
    @Override
    public int insertBaseDataMember(BaseDataMember baseDataMember)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        baseDataMember.setId("10" + formattedDate+randomNumber);
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataMember.setCreateById(loginUser.getUser().getUserId().toString());
        baseDataMember.setCreateBy(loginUser.getUser().getUserName());
        baseDataMember.setCreateTime(DateUtils.getNowDate());
        return baseDataMemberMapper.insertBaseDataMember(baseDataMember);
    }

    /**
     * 修改会员
     *
     * @param baseDataMember 会员
     * @return 结果
     */
    @Override
    public int updateBaseDataMember(BaseDataMember baseDataMember)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataMember.setUpdateById(loginUser.getUser().getUserId().toString());
        baseDataMember.setUpdateBy(loginUser.getUser().getUserName());
        baseDataMember.setUpdateTime(DateUtils.getNowDate());
        return baseDataMemberMapper.updateBaseDataMember(baseDataMember);
    }

    /**
     * 批量删除会员
     *
     * @param ids 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataMemberByIds(String[] ids)
    {
        return baseDataMemberMapper.deleteBaseDataMemberByIds(ids);
    }

    /**
     * 删除会员信息
     *
     * @param id 会员主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataMemberById(String id)
    {
        return baseDataMemberMapper.deleteBaseDataMemberById(id);
    }
}
